//! By convention, main.zig is where your main function lives in the case that
//! you are building an executable. If you are making a library, the convention
//! is to delete this file and start with root.zig instead.

pub fn main() !void {
    // 使用模型，一定要是变量，不能是常量
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    // 拿到一个allocator
    const allocator = gpa.allocator();

    // defer 用于执行general_purpose_allocator善后工作
    defer {
        // 尝试进行 deinit 操作
        const deinit_status = gpa.deinit();

        // 检测是否发生内存泄漏
        if (deinit_status == .leak) @panic("TEST FAIL");
    }

    //申请内存
    const bytes = try allocator.alloc(u8, 100);
    // 延后释放内存
    defer allocator.free(bytes);
}

test "simple test" {
    var list = std.ArrayList(i32).init(std.testing.allocator);
    defer list.deinit(); // Try commenting this out and see if zig detects the memory leak!
    try list.append(42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

test "use other module" {
    try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50));
}

test "fuzz example" {
    const Context = struct {
        fn testOne(context: @This(), input: []const u8) anyerror!void {
            _ = context;
            // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
            try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
        }
    };
    try std.testing.fuzz(Context{}, Context.testOne, .{});
}

const std = @import("std");

/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
const lib = @import("demo6_lib");
